package com.hehui.partimejob.adapter;

import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

public abstract class MyRecyclerViewAdapter<T> extends RecyclerView.Adapter<MyRecyclerViewAdapter.InnerHolder> {
    private final List<T> list;
    private final int layoutId;


    public MyRecyclerViewAdapter(List<T> list, int layoutId) {
        this.list = list;
        this.layoutId = layoutId;
    }

    @NonNull
    @Override    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new InnerHolder(LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false));
    }

    @Override
    public int getItemCount() {
        return list

                .size();
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        handleData(holder, list.get(position));
    }

    protected abstract void handleData(InnerHolder holder, T item);

    public static class InnerHolder extends RecyclerView.ViewHolder {
        private SparseArray<View> sparseArray = new SparseArray<>();

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }

        public <T extends View> T getView(int id) {
            View view = sparseArray.get(id);
            if (view == null) {
                view = itemView.findViewById(id);
                sparseArray.put(id, view);
            }
            return (T) view;
        }

        public InnerHolder setText(int id, String str) {
            TextView textView = getView(id);
            textView.setText(str);
            return this;
        }   public InnerHolder setVisibility(int id,boolean flag) {
            getView(id).setVisibility(flag?View.VISIBLE:View.GONE);
            return this;
        }

        public InnerHolder setBg(int id, int res) {
            getView(id).setBackgroundResource(res);
            return this;
        }

        public InnerHolder setImg(int id, String res) {
            ImageView view = getView(id);
            Glide.with(view).load(res).into(view);
            return this;
        }public InnerHolder setImgDrawable(int id, Drawable res) {
            ImageView view = getView(id);
            Glide.with(view).load(res).into(view);
            return this;
        }public InnerHolder setTag(int id, Object tag) {
            getView(id).setTag(tag);
            return this;
        }

        public InnerHolder setOnClickListener(int id, View.OnClickListener res) {
            getView(id).setOnClickListener(res);
            return this;
        }

        public InnerHolder setBgDrawable(int id, Drawable pic) {
            ImageView view = getView(id);
            view.setBackground(pic);
            return this;
        }
    }
}
